﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using PBOffice.BLL;
using AjaxControlToolkit;
using PBOffice.DAL;

public partial class MasterPage_PBOAMasterPage : System.Web.UI.MasterPage
{
	User um = null;
	protected void Page_Init()
	{
		//List<PBOffice.DAL.User> um = (List<PBOffice.DAL.User>)Session["userManage"];
		um = LoginBLL.SelectUserManageByUserIdAndUserPwd("admin", "123321");
		Session["userManage"] = um;
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		LabelUserName.Text = "当前用户：" + um.UserName;
		int userNo = um.UserNo;
		var menu = MasterPageBLL.SelectMenuByUserNo(userNo);
		//手风琴绑定数据
		AccordionShow(menu);
		if (Request.Cookies["accordion"] != null)
		{
			AccordionMenu.SelectedIndex = Convert.ToInt32(Request.Cookies["accordion"].Value);
		}

		//if (this.Page.PreviousPage != null && this.Page.PreviousPage.IsCrossPagePostBack)
		//{
		//    Accordion a = (Accordion)this.Page.PreviousPage.Master.FindControl("AccordionMenu");
		//    AccordionMenu.SelectedIndex = a.SelectedIndex;
		//}
		this.Page.ClientScript.RegisterStartupScript(this.GetType(), "timer", "(function showTime(){DesktopService.GetNowTime(function (r) { $get('time').innerHTML = r; });setTimeout(showTime,1000);})();", true);
	}


	#region 菜单列表数据绑定
	/// <summary>
	/// 菜单列表数据绑定
	/// </summary>
	/// <param name="menu"></param>
	private void AccordionShow(List<PBOffice.DAL.v_SelectUserRole> menu)
	{
		for (int i = 0; i < menu.Count; i++)
		{
			//获取返回的数据
			PBOffice.DAL.v_SelectUserRole View = menu[i];
			//判断是否为根节点
			if (View.FatherNodeNo == 0)
			{
				//声明一个AccordionPane 容器
				AccordionPane ap = new AccordionPane();
				//给AccordionPane 添加唯一的ID
				//因为这个版本的ajax程序集有问题，需要添加唯一的标识
				ap.ID = View.MenuItemNo.ToString();
				Label lab = new Label();
				lab.Text = View.MenuItemId.ToString();
				//加添header部分
				ap.HeaderContainer.Controls.Add(lab);
				//获取到父节点下的子节点
				var son = from s in menu
						  where s.FatherNodeNo == Convert.ToInt32(ap.ID)
						  select s;
				//遍历添加子节点
				foreach (var item in son)
				{
					///定义链接
					LinkButton link = new LinkButton();
					link.CausesValidation = false;
					link.Text = item.MenuItemId.ToString();
					link.CssClass = "linkbtn";
					link.Click += new EventHandler(link_Click);
					link.ID = item.Url.ToString();
					Panel div = new Panel();
					div.Controls.Add(link);
					div.CssClass = "divLinkBtn";
					//添加到content 部分
					ap.ContentContainer.Controls.Add(div);
				}
				//添加唯一标识ID
				ap.HeaderContainer.ID = "ahc" + i;
				ap.ContentContainer.ID = i.ToString();
				//添加到控件中
				AccordionMenu.Panes.Add(ap);
			}
		}
	}

	void link_Click(object sender, EventArgs e)
	{
		LinkButton link = sender as LinkButton;
		string a = link.Parent.Parent.ID.ToString();
		Response.Cookies["accordion"].Value = a;
		Response.Redirect(link.ID.ToString());
	}
	#endregion


	protected void LinkButtonExit_Click(object sender, EventArgs e)
	{
		//获取保存的信息 
		var user = (PBOffice.DAL.User)Session["userManage"];
		DateTime loginLogs = (DateTime)Session["loginTime"];
		MasterPageBLL.AddOutLoginLogs(loginLogs, user);
		Session["userManage"] = null;
		Response.Redirect("~/login.aspx");
	}
}

